home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BBS in a Box 3
/
BBS in a box - Trilogy III.iso
/
Files
/
Prog
/
B-C
/
C++ FAQ Reference 1.0
/
C++ FAQ Reference 1.0.rsrc
/
TEXT_1132.txt
< prev
next >
Encoding:
Amiga
Atari
Commodore
DOS
FM Towns/JPY
Macintosh
Macintosh JP
Macintosh to JP
NeXTSTEP
RISC OS/Acorn
Shift JIS
UTF-8
Wrap
Text File
|
1993-06-30
|
505 b
|
10 lines
Provide a friend operator<< that calls a protected virtual function:
class X {
protected:
virtual void print(ostream& o) const; //or '=0;' if 'X' is abstract
public:
friend ostream& operator<<(ostream& o,const X& x) {x.print(o); return o;}
//...
};
Now all subclasses of X merely provide their own 'print(ostream&)const' member function, and they all share the common '<<' operator. Friends don't bind dynamically, but this technique makes them *act* as if they were.